import 'package:flutter/material.dart';

///对话框背景与拦截
class DialogBg extends StatelessWidget {
  const DialogBg({
    Key? key,
    required this.child,
    this.allowBack = true,
  }) : super(key: key);

  ///子控件
  final Widget child;

  ///允许返回
  final bool allowBack;

  ///返回拦截
  Future<bool> _onWillPop(BuildContext context) async {
    return await Future<bool>.value(allowBack);
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      child: GestureDetector(
        onTap: () {
          if (allowBack) Navigator.pop(context);
        },
        child: Material(
          color: Colors.transparent,
          child: Center(child: child),
        ),
      ),
      onWillPop: () async => await _onWillPop(context),
    );
  }
}
